﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;


namespace Quest
{
    abstract class Mover
    {
        private const int MoveInterval = 10;
        protected Point location;
        public Point Location { get { return location; } }
        protected Game game;

        public Mover(Game game, Point location)
        {
            this.game = game;
            this.location = location;
        }

        public bool Nearby(Point locationToCheck, int distance)
        {
            return Nearby(location, locationToCheck, distance);
            //return (Math.Abs(location.X - locationToCheck.X) < distance && (Math.Abs(location.Y - locationToCheck.Y) < distance));
        }

        public bool Nearby(Point point1, Point point2, int distance)
        {
            return getDistance(point1, point2)<=distance*distance;
        }

        private int getDistance(Point point1, Point point2)
        {
            int dX = point1.X - point2.X;
            int dY = point1.Y - point2.Y;
            return dX * dX + dY * dY;
        }

        public Point Move(Direction direction, Rectangle boundaries)
        {
            return Move(direction, location, boundaries);

            //Point newLocation = location;
            //switch (direction)
            //{
            //    case Direction.Up:
            //        if (newLocation.Y - MoveInterval >= boundaries.Top)
            //            newLocation.Y -= MoveInterval;
            //        break;
            //    case Direction.Down:
            //        if (newLocation.Y + MoveInterval <= boundaries.Bottom)
            //            newLocation.Y += MoveInterval;
            //        break;
            //    case Direction.Left:
            //        if (newLocation.X - MoveInterval >= boundaries.Left)
            //            newLocation.X -= MoveInterval;
            //        break;
            //    case Direction.Right:
            //        if (newLocation.X + MoveInterval <= boundaries.Right)
            //            newLocation.X += MoveInterval;
            //        break;
            //    default: break;
            //}
            //return newLocation;
        }

        public Point Move(Direction direction, Point target, Rectangle boundaries)
        {
//            Point newLocation = location;
            Point newLocation = target;
            switch (direction)
            {
                case Direction.Up:
                    if (newLocation.Y - MoveInterval >= boundaries.Top)
                        newLocation.Y -= MoveInterval;
                    break;
                case Direction.Down:
                    if (newLocation.Y + MoveInterval <= boundaries.Bottom)
                        newLocation.Y += MoveInterval;
                    break;
                case Direction.Left:
                    if (newLocation.X - MoveInterval >= boundaries.Left)
                        newLocation.X -= MoveInterval;
                    break;
                case Direction.Right:
                    if (newLocation.X + MoveInterval <= boundaries.Right)
                        newLocation.X += MoveInterval;
                    break;
                default: break;
            }
            return newLocation;
        }


    }
}
